#!/usr/bin/perl
#
# svn user ui
#

#login cleck
require "./auth.cgi";

use CGI;
use cfg;
use SvnRep;

my $svn_url=cfg::get('svn_url');
my $q=CGI->new;
$q->header(-charset=>'utf-8');

#
# Default HTML
#
$html=<<"END";
<html>
	<head>
		<title>SVN User</title>
		<style type="text/css">
			body{background-color:#5D9041;margin:0;padding:0;}
			#bar{width:100%;background-color:#163E43;height:45px;padding-top:5px;}
			#bar span{font-size:2em;font-weight:bold;color:#FFF;margin-left:1em;}
			#link{float: right; height: 30px; line-height: 40px; margin-top: 15px; margin-right: 50px;}
			#link a,#link a:visited,#link a:active,#link a:hover{text-decoration:none;padding:0px 0.2em;margin-left:0.2em;}
			#link a,#link a:visited{background-color:#3274C8;color:#FFF;}
			#link a:hover{background-color:#90C53F;}
			#link font{color:FFF;}
			#msg{
				width:500px;
				margin:auto;
				margin-top:50px;
				background-color:#8BC6BE;
				padding:1em;
				font-size:2em;
				border:1px solid #066A4B;
			}
			#chg_pwd{
				width:400px;
				margin:auto;
				margin-top:50px;
				background-color:#ADD8C7;
				padding:2em;
			}
			#chg_pwd h3{text-align:center;}
			#rep_list{width:80%;margin:30px 10% 0;background-color:#6DB2A2;}
#mylist th{border-bottom:1px solid #000;}
#mylist td,#rep_list th{border-left:1px solid #000;}
#mylist th:first-child,#rep_list td:first-child{border-left:0;}
#mylist tr:hover{background-color:#39B95F;}
#mylist{width:100%;border-collapse:collapse;}
#mylist td{padding:0.5em 0.3em;}


		</style>
	</head>
	<body>
		<div id="bar">
			<span>SVN User</span>
			<div id="link">
				<font>Login: %s</font>
				<a href="?active=chg_pwd">Change Password</a>
				<a href="./logout.cgi">Logout</a>
			</div>
		</div>
		<div id="main">%s</div>
	</body>
</html>
END

$active=$q->param('active');
#
# Change Password
#
if($active eq "chg_pwd"){
$main=<<"END";
	<div id="chg_pwd">
		<h3>Change Password</h3>
		<form action="#" method="post">
			<input type="hidden" name="active" value="chg_pwd">
			<p>New Password:<input type="password" name="new_pw" /></p>
			<p>Re-Enter New Password:<input type="password" name="new_pw2"></p>
			<input type="submit" value="Change Password" />
			<input type="button" value="Cancel" onclick="location.href='./svnuser.cgi'" />
		</form>
	</div>
END
	$new_pw=$q->param('new_pw');
	$new_pw2=$q->param('new_pw2');
	print "<h1>$new_pw,$new_pw2</h1>";
	if($new_pw ne $new_pw2){
		$main='<div id="msg"><h3>Password Not Match</h3></div>'.$main;
	}elsif($new_pw ne ""){
		$rt=SvnUser::chg_mypwd($cookie_uid,$cookie_pwd,$new_pw);
		if($rt eq 1){
			SvnRep::set_pwd_allrep($cookie_uid,$new_pw);
			$main='<div id="msg">Change Password Success.<br />Please use New password login.</div>';
		}else{
			$main=$rt.$main;
			$main='<div id="msg">Change Password Failed</div>'.$main;
		}
	}
}else{

#
#Default main
#
$list='';
$tmp=<<"END";
<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>
END
@myrep=SvnRep::get_user_in_rep($cookie_uid);
foreach $myrep (@myrep){
	$list.=sprintf($tmp,$myrep,SvnRep::get_rep_context($myrep),SvnRep::get_rep_rev($myrep),sprintf($svn_url,$myrep));
}
$main=<<"END";
<div id="rep_list">
	<table id="mylist">
		<tr>
			<th>Name</th>
			<th>Context</th>
			<th>Rev</th>
			<th>URL</th>
		</tr>%s</talbe>
</div>
END

$main=sprintf($main,$list);

}


#
# Output HTML
#
print $q->header();
printf($html,$cookie_uid,$main);
